using System;
using System.Data;
using System.Text;
using System.Collections;

namespace Adoor.Domain.Tiers
{


    public abstract class Role_Assure
    {


        public virtual Personne PersonnePhysique
        {
            get
            {
                return this.m_personnePhysique;
            }
            set
            {
                this.m_personnePhysique = value;
            }
        }

        
        public virtual InfoComplementaire InfoComplementaire
        {
            get
            {
                return this.m_InfoComplementaire;
            }
            set
            {
                this.m_InfoComplementaire = value;
            }
        }        


        //public virtual IList/*SACIJO.Domain.Retraite.Dossier.Document */ Documents
        //{
        //    get { return this.m_Documents; }
        //}


        //public virtual IList/*SACIJO.Domain.Retraite.Dossier.LigneHistorique */ Historique
        //{
        //    get { return this.m_Historique; }
        //}

        public virtual System.Collections.Generic.IList<Role_Conjoint> Conjoints
        {
            get { return this.m_Conjoints; }
        }

        public virtual System.Collections.Generic.IList<Role_Enfant> Enfants
        {
            get { return this.m_Enfants; }
        }

        public virtual String Login
        {
            get
            {
                return this.m_login;
            }
            set
            {
                this.m_login = value;
            }
        }


        //public virtual IList/*SACIJO.Domain.Retraite.Dossier.SimulationRetraite*/ SimulationRetraite
        //{
        //    get { return this.m_SimulationRetraite; }
        //}

        //public virtual Dossier.DossierRetraite DossierRetraite
        //{
        //    get
        //    {
        //        return this.m_DossierRetraite;
        //    }
        //    set
        //    {
        //        this.m_DossierRetraite = value;
        //    }
        //}

        private string CryptString(string value)
        {
            string crypt = String.Empty; 
            System.Security.Cryptography.MD5CryptoServiceProvider provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
            try
            {
                byte[] cryptBytes = provider.ComputeHash(Encoding.UTF8.GetBytes(value));
                crypt = Convert.ToBase64String(cryptBytes);
            }
            finally { provider.Clear(); }

            return crypt; 
        }

        public event PasswordEventHandler PasswordChanging;

        protected void OnPassWordChanging(MotDePassEventArg e)
        {
            if (PasswordChanging != null)
                PasswordChanging(this,e);
        }

        public virtual String MotDePass
        {
            get
            {
                return this.m_motdepass;
            }
            set
            {
                if (value != null && value != this.m_motdepass)
                { 
                    MotDePassEventArg e = new MotDePassEventArg();
                    e.MotDePass = value;
                    //OnPassWordChanging(e);
                    string crypt = CryptString(value);
                    this.m_motdepass = crypt;
                }
            }
        }

        public virtual bool CompteBloque
        {
            get
            {
                return m_CompteBloque;
            }
            set
            {
                m_CompteBloque = value;
            }
        }

        public virtual int NbTentatives
        {
            get {
                return this.m_NbTentatives; 
            }
            set
            {
                this.m_NbTentatives = value;
            }
        }

      
        
        public virtual String NomOrganismeSuccession
        {
            get
            {
                return this.m_NomOrganismeSuccession;
            }
            set
            {
                this.m_NomOrganismeSuccession = value;
            }
        }


        public virtual bool Societaire
        {
            get
            {
                if (m_personnePhysique.ExternalBdd)
                    return this.m_local_Societaire;
                else
                    return this.m_Societaire;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd)
                    this.m_local_Societaire = value;
                else
                    this.m_Societaire = value;
            }
        }

        public virtual DateTime DateEntreeJO
        {
            get
            {
                if (m_personnePhysique.ExternalBdd)
                    return this.m_local_DateEntreeJO;
                else
                    return this.m_DateEntreeJO;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd)
                    this.m_local_DateEntreeJO = value;
                else
                    this.m_DateEntreeJO = value;
            }
        }

        public virtual DateTime DateSortieJO
        {
            get
            {
                if (m_personnePhysique.ExternalBdd)
                    return this.m_local_DateSortieJO;
                else
                    return this.m_DateSortieJO;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd)
                    this.m_local_DateSortieJO = value;
                else
                    this.m_DateSortieJO = value;
            }
        }

        public virtual DateTime DateDebutCarriere
        {
            get
            {
                    return this.m_DateDebutCarriere;
            }
            set
            {
                    this.m_DateDebutCarriere = value;
            }
        }

        public virtual DateTime DatePietonnage
        {
            get
            {
                if (m_personnePhysique.ExternalBdd)
                    return this.m_local_DatePietonnage;
                else
                    return this.m_DatePietonnage;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd)
                    this.m_local_DatePietonnage = value;
                else
                    this.m_DatePietonnage = value;
            }
        }

        public virtual DateTime DateDebutPreretraite
        {
            get
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P" )
                    return this.m_local_DateDebutPreretraite;
                else
                    return this.m_DateDebutPreretraite;
            }
            set
            {                
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P")
                    this.m_local_DateDebutPreretraite = value;
                else
                    this.m_DateDebutPreretraite = value;
            }
        }


        public virtual DateTime DateMensualisation
        {
            get
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "S")
                    return this.m_local_DateMensualisation;
                else
                    return this.m_DateMensualisation;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "S")
                    this.m_local_DateMensualisation = value;
                else
                    this.m_DateMensualisation = value;
            }
        }

        public virtual DateTime DateEntreePension
        {
            get
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P")
                    return this.m_local_DateEntreePension;
                else
                    return this.m_DateEntreePension;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P")
                    this.m_local_DateEntreePension = value;
                else
                    this.m_DateEntreePension = value;
            }
        }
        
        public virtual DateTime DateFinPreretraite
        {
            get
            {  
                    return this.m_DateFinPreretraite;
            }
            set
            {
                    this.m_DateFinPreretraite = value;
            }
        }

        public virtual DateTime DateSocietaire
        {
            get
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P")
                    return this.m_local_DateSocietaire;
                else
                    return this.m_DateSocietaire;
            }
            set
            {
                if (m_personnePhysique.ExternalBdd && m_personnePhysique.Origine != "P")
                    this.m_local_DateSocietaire = value;
                else
                    this.m_DateSocietaire = value;
            }
        }

        /// <summary>
        /// Called when user is creating a new entity.
        /// </summary>
        protected virtual void OnCreate(object[] args)
        {
            this.m_Societaire = false;
            this.Login = String.Empty;
            this.m_NbTentatives = 0;
            this.CompteBloque = false;
            this.Login = string.Empty;
            this.MotDePass = string.Empty;
            this.NomOrganismeSuccession = string.Empty;

            this.m_DateEntreeJO = new DateTime(1800, 1, 1);
            this.m_DateSortieJO = new DateTime(1800, 1, 1);
            this.DateDebutCarriere = new DateTime(1800, 1, 1);
            this.m_DatePietonnage = new DateTime(1800, 1, 1);
            this.DateFinPreretraite = new DateTime(1800, 1, 1);
            this.m_DateDebutPreretraite = new DateTime(1800, 1, 1);
            this.m_DateMensualisation = new DateTime(1800, 1, 1);
            this.m_DateEntreePension = new DateTime(1800, 1, 1);
            this.m_DateSocietaire = new DateTime(1800, 1, 1);
        }

        /// <summary>
        /// Called when user is retreiving an entity from storage
        /// </summary>
        protected virtual void OnMaterialize()
        {

        }

        protected abstract InfoComplementaire m_InfoComplementaire { get; set; }
        //protected abstract IList m_Documents { get; }
        //protected abstract IList m_Historique { get; }
        protected abstract Personne m_personnePhysique { get; set; }
        protected abstract System.Collections.Generic.IList<Role_Conjoint> m_Conjoints { get; }
        protected abstract System.Collections.Generic.IList<Role_Enfant> m_Enfants { get; }
        protected abstract System.String m_login { get; set; }
        protected abstract System.String m_motdepass { get; set; }
        protected abstract System.Boolean m_CompteBloque { get;set;}
        protected abstract System.Int32 m_NbTentatives { get; set;}
        protected abstract System.String m_NomOrganismeSuccession { get;set;}
        protected abstract System.Boolean m_Societaire { get;set;}        
        //protected abstract Dossier.DossierRetraite m_DossierRetraite { get;set;}
        //protected abstract IList m_SimulationRetraite { get; }
        
        protected abstract System.DateTime m_DateEntreeJO { get;set;}
        protected abstract System.DateTime m_DateSortieJO { get;set;}
        protected abstract System.DateTime m_DateDebutCarriere { get;set;}
        protected abstract System.DateTime m_DatePietonnage { get;set;}
        protected abstract System.DateTime m_DateDebutPreretraite { get;set;}
        protected abstract System.DateTime m_DateFinPreretraite { get;set;}
        protected abstract System.DateTime m_DateMensualisation { get;set;}
        protected abstract System.DateTime m_DateEntreePension { get;set;}
        protected abstract System.DateTime m_DateSocietaire { get;set;}
        
        /* Local storage */
        private System.Boolean m_local_Societaire = false;
        private System.DateTime m_local_DateEntreeJO = new DateTime(1800,1,1);
        private System.DateTime m_local_DateSortieJO = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateDebutCarriere = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DatePietonnage = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateDebutPreretraite = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateFinPreretraite = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateMensualisation = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateEntreePension = new DateTime(1800, 1, 1);
        private System.DateTime m_local_DateSocietaire = new DateTime(1800, 1, 1);
    }
}

public class MotDePassEventArg : EventArgs
{
    public String MotDePass;
}

public delegate void PasswordEventHandler(object sender, MotDePassEventArg e);